package com.example.common;


import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import static com.example.common.Code.SERVER_ERROR;


@Data
public class JsonResult<T> {

    private boolean success;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer errorCode;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String errorMessage;

    public static <T> JsonResult<T> ok() {
        JsonResult<T> result = new JsonResult<>();
        result.setSuccess(true);
        return result;
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> result = new JsonResult<>();
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    public static <T> JsonResult<T> error(Code code) {
        JsonResult<T> result = new JsonResult<>();
        result.setErrorCode(code.getCode());
        result.setErrorMessage(code.getMessage());
        return result;
    }

    public static <T> JsonResult<T> error(String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setErrorCode(SERVER_ERROR.getCode());
        result.setErrorMessage(message);
        return result;
    }

    public static <T> JsonResult<T> error() {
        JsonResult<T> result = new JsonResult<>();
        result.setErrorCode(SERVER_ERROR.getCode());
        result.setErrorMessage(SERVER_ERROR.getMessage());
        return result;
    }

    public static <T> JsonResult<T> error(Integer code ,String message) {
        JsonResult<T> result = new JsonResult<>();
        result.setErrorCode(code);
        result.setErrorMessage(message);
        return result;
    }
}